﻿Imports BUS
Imports DTO
Imports System.Threading

Public Class frmDangNhap

    Private Shared maND As String
    Private dsTaiKhoang As List(Of NguoiDungDTO)
    Private _load As Boolean = False

    Shared Sub New()
        DevExpress.UserSkins.BonusSkins.Register()
        DevExpress.Skins.SkinManager.EnableFormSkins()
    End Sub
    Public Shared Sub Thear()
        Dim frm As New frmMain()
        frmMain.maND = maND
        Application.Run(frm)
        'SplashScreenManager1.CloseWaitForm()
    End Sub
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        DevExpress.UserSkins.BonusSkins.Register()

        DevExpress.LookAndFeel.UserLookAndFeel.Default.UseWindowsXPTheme = False
        DevExpress.LookAndFeel.UserLookAndFeel.Default.UseDefaultLookAndFeel = True
        DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName = "Blue"

        DevExpress.Skins.SkinManager.EnableMdiFormSkins()
        DevExpress.Skins.SkinManager.EnableFormSkins()
        ' Add any initialization after the InitializeComponent() call.

    End Sub


    Private Sub txtDangNhap_Click(sender As Object, e As EventArgs) Handles txtDangNhap.Click
        SplashScreenManager1.ShowWaitForm()
        If kiemTraDangNhap() = True Then
            maND = DangNhapBUS.DangNhap(cbbUserName.Text, txtPass.Text)
            If maND <> "0" And maND <> "-1" Then
                If ckbNhoPass.Checked = True Then
                    DangNhapBUS.ghiNhoMatKhau(cbbUserName.Text.ToString, txtPass.Text)
                End If
                HeThongBUS.themNhatKy(maND, "Hệ Thống", "Đăng Nhập", "")

                SplashScreenManager1.CloseWaitForm()
                Me.Close()
                Dim t As New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf Thear))
                t.SetApartmentState(System.Threading.ApartmentState.STA)
                t.Start()

            Else
                If maND = "-1" Then
                    DevExpress.XtraEditors.XtraMessageBox.Show("Tài khoản này đã bị khóa", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    SplashScreenManager1.WaitForSplashFormClose
                    SplashScreenManager1.CloseWaitForm()
                Else
                    DevExpress.XtraEditors.XtraMessageBox.Show("Tên đăng nhập và mật khẩu sai", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    SplashScreenManager1.CloseWaitForm()
                    SplashScreenManager1.WaitForSplashFormClose()
                End If
            End If
        End If

    End Sub
    Private Function kiemTraDangNhap() As Boolean
        If cbbUserName.Text.ToString.Length <= 0 Then
            DevExpress.XtraEditors.XtraMessageBox.Show("Tên đăng nhập không được để trống", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End If
        'If txtPass.Text.ToString.Length <= 0 Then
        '    DevExpress.XtraEditors.XtraMessageBox.Show("Mật Khẩu không được để trống", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        'End If
        Return True
    End Function

    Private Sub frmDangNhap_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        dsTaiKhoang = DangNhapBUS.docDsTaiKhoanDaLuu()
        If dsTaiKhoang.Count >= 0 Then
            For Each nd As NguoiDungDTO In dsTaiKhoang
                cbbUserName.Properties.Items.Add(nd.TaiKhoan)
            Next
        End If
        _load = True
    End Sub

    Private Sub cbbUserName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbbUserName.SelectedIndexChanged
        If _load Then
            Try
                txtPass.Text = dsTaiKhoang.Find(Function(value As NguoiDungDTO)
                                                    Return value.TaiKhoan = cbbUserName.SelectedText
                                                End Function).MatKhau
            Catch ex As Exception

            End Try

        End If

    End Sub


End Class